<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCartsTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up()
    {
        Schema::create('carts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->comment('用户ID');
            $table->unsignedBigInteger('product_id')->comment('商品ID');
            $table->integer('quantity')->default(1)->comment('商品数量');

            // 商品规格存储（JSON格式）
            $table->json('sku')->nullable()->comment('商品规格');

            // 状态管理
            $table->unsignedTinyInteger('selected')->default(1)->comment('选中状态 0=未选中 1=选中');
            $table->unsignedTinyInteger('is_valid')->default(1)->comment('有效性 0=失效 1=有效');
            $table->index(['user_id', 'is_valid'], 'user_valid_index');
            $table->timestamps();
        });
        \Illuminate\Support\Facades\DB::statement("ALTER TABLE `carts` comment '购物车'");
    }

    /**
     * Reverse the migrations.
     */
    public function down()
    {
        Schema::dropIfExists('carts');
    }
}
